<?php
 /******************************************************************************
 *   GunCMS is an avid supporter of open source software. This is the appropriate 
 *   option if you are creating an open source application with a license 
 *   compatible with the GNU GPL license v3.
 *   For details http://www.gnu.org/licenses/gpl.html
 *   DO NOT USE FOR COMMERCIAL WITHOUT PERMISSION
 *
 * GunCMS Version 1.0.3
 * Copyright(c) 2009-2010, T & L GdbR
 * info@guncms.de
 * 
 * http://www.guncms.de
 ********************************************************************************/
class BWebshopCustomerBean {
	
	  public function __construct() {
	  //
	  }
		
	  public function __destruct() {
		//
	  }
	  
	 
	  
	  public static function get_customer_by_id($db, $id) {
    	$strSQL = "SELECT DATE_FORMAT(c.customer_dob, '%d.%m.%Y') as dob_date,c.* FROM ".TBL_CUSTOMER." c ".
						"WHERE c.customer_id='$id'";
    	return $db->get_row($strSQL);
    }
    
    public static function save_customer($db, $info) {
    	$sql=sprintf(
					"INSERT INTO %s(customer_gender,customer_firstname,customer_lastname,customer_dob,customer_company,".
					"delivery_gender,delivery_firstname,delivery_lastname,delivery_street_address,delivery_postcode,delivery_city,delivery_state,delivery_country,".
					"billing_gender,billing_firstname,billing_lastname,billing_street_address,billing_postcode,billing_city,billing_state,billing_country,".
					"customer_email,customer_telephone,customer_cellphone,customer_fax,customer_password,customer_newsletter,customer_registered) " .
					"VALUES('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s',now())",
    				TBL_CUSTOMER, 
    				$info['customer_gender'],
    				$info['customer_firstname'],
    				$info['customer_lastname'],
					$info['customer_dob'],
					$info['customer_company'],
					$info['delivery_gender'],
					$info['delivery_firstname'],
    				$info['delivery_lastname'],
					$info['delivery_street_address'],
					$info['delivery_postcode'],
					$info['delivery_city'],
					$info['delivery_state'],
					$info['delivery_country_id'],
					$info['billing_gender'],
					$info['billing_firstname'],
    				$info['billing_lastname'],
					$info['billing_street_address'],
					$info['billing_postcode'],
					$info['billing_city'],
					$info['billing_state'],
					$info['billing_country_id'],
					$info['customer_email'],
					$info['customer_telephone'],
					$info['customer_cellphone'],
					$info['customer_fax'],
					crypt($info['customer_password'], '$ntp$'),
    				$info['customer_newsletter']);
		  
        		
    	$res = $db->query($sql);
		$id = -1;
		if($res == true) {	
			$id=$db->insert_id;
			return $id;
		}	
    	else
    		return $id;
    }
    
    public static function update_customer($db, $info) {
    	$sql = sprintf("UPDATE %s SET " .
    			"customer_gender = '%s', " .
    			"customer_firstname = '%s', " .
    			"customer_lastname = '%s', " .
    			"customer_dob = '%s', " .
				"delivery_gender = '%s', " .
    			"delivery_firstname = '%s', " .
    			"delivery_lastname = '%s', " .
				"delivery_street_address = '%s', " .
    			"delivery_postcode = '%s', " .
    			"delivery_city = '%s', " .
    			"delivery_state = '%s', " .
    			"delivery_country = '%s', " .
				"billing_gender = '%s', " .
    			"billing_firstname = '%s', " .
    			"billing_lastname = '%s', " .
				"billing_street_address = '%s', " .
    			"billing_postcode = '%s', " .
    			"billing_city = '%s', " .
    			"billing_state = '%s', " .
    			"billing_country = '%s', " .
    			"customer_company = '%s', " .
    			"customer_email = '%s', " .
    			"customer_telephone = '%s', ".
				"customer_cellphone = '%s', ".
    			"customer_fax = '%s', " .
    			"customer_password = '%s', " .
    			"customer_newsletter = '%s' ," .
    			"customer_registered = now() " .
    			"WHERE  customer_id =" . $info['customer_id'],
    			TBL_CUSTOMER,
    			$info['customer_gender'],
    			$info['customer_firstname'],
    			$info['customer_lastname'],
    			$info['customer_dob'],
				$info['delivery_gender'],
    			$info['delivery_firstname'],
    			$info['delivery_lastname'],
				$info['delivery_street_address'],
    			$info['delivery_postcode'],
    			$info['delivery_city'],
    			$info['delivery_state'],
    			$info['delivery_country_id'],
				$info['billing_gender'],
    			$info['billing_firstname'],
    			$info['billing_lastname'],
				$info['billing_street_address'],
    			$info['billing_postcode'],
    			$info['billing_city'],
    			$info['billing_state'],
    			$info['billing_country_id'],
    			$info['customer_company'],
    			$info['customer_email'],
    			$info['customer_telephone'],
				$info['customer_cellphone'],
    			$info['customer_fax'],
    			$info['customer_password'],
    			$info['customer_newsletter']);
    	$res = $db->query($sql);

    	return $res;
    }
    public static function delete_customer_by_id($db, $id) {
		/*$info = $db->get_results("SELECT o.so_id FROM " . TBL_ORDER_INFO . " o WHERE o.customer_id = '$id'");
		if($info!=""){
			foreach($info as $item){
				$soid = $item->so_id;
				$db->query(sprintf("DELETE FROM %1\$s WHERE so_id='$soid'", TBL_ORDER_PRODUCT));	
			}
		}*/
		//$db->query(sprintf("DELETE FROM %1\$s WHERE customer_id ='$id'", TBL_ORDER_INFO));	
    	$strSQL = sprintf("DELETE FROM %1\$s WHERE customer_id='$id'", TBL_CUSTOMER);
    	$res = $db->query($strSQL);
    	return $res;
    }
    
    public static function existed_customer_by_email($db, $email) {
    	$strSQL = sprintf("SELECT count(%1\$s.customer_id) FROM %1\$s WHERE customer_email='$email'", TBL_CUSTOMER);
    	$count = $db->get_var($strSQL);
    	return ($count > 0);
    }
    
	public function getCustomerPassword($db, $id)
    {
    	$sql = sprintf("SELECT customer_password FROM " . TBL_CUSTOMER . " WHERE customer_id = $id");
    	$pass = $db->get_row($sql);
    	return $pass->customer_password;
    }
	
    public static function get_all_customer_paging($db,$paging,$path,$sort,$by) {
    	if($sort!="")
			$condsort = "ORDER BY cus.".$sort." ".$by;
		else
			$condsort = "ORDER BY cus.customer_id ";
        $page = isset ( $_GET[$path]) ? intval ( $_GET[$path] ) : 1;
        $rows_per_page= $paging;
        $page_start = ( $page - 1 ) * $rows_per_page;
        $page_end = $page * $rows_per_page;
        $strSQL = "SELECT * FROM " . TBL_CUSTOMER . " cus $condsort LIMIT $page_start,$rows_per_page ";
        return $db->get_results($strSQL);
    }
    
	  /**
     * COUNT all customers in database.
     * @return total of customers.
     * Notes: this data is viewed in Overview.  
     */
    public static function count_all_customer($db) {
        $strSQL = sprintf("SELECT COUNT(%1\$s.customer_id) as total FROM %1\$s ", TBL_CUSTOMER);
        $res = $db->get_row($strSQL);
		    return	$res->total;
    }
	
	 public function getCustomerByEmail($db, $customer_email,$postemail) {
	 	if($postemail!="")
			$cond = " AND customer_email!='$postemail' ";	
    	$sql = sprintf("SELECT * FROM ".TBL_CUSTOMER." WHERE customer_email='$customer_email' $cond AND customer_active ='1'");
    	return $db->get_row($sql);
    }
	public function getCustomerEmail($db, $id){
    	$sql = sprintf("SELECT customer_email FROM " . TBL_CUSTOMER . " WHERE customer_id = $id");
    	$customeremail = $db->get_row($sql);
    	return $customeremail->customer_email;
    }
}
?>    